package cn.wolfcode.car.business.service.impl;


import cn.wolfcode.car.business.domain.Appointment;
import cn.wolfcode.car.business.domain.Statement;
import cn.wolfcode.car.business.mapper.AppointmentMapper;
import cn.wolfcode.car.business.query.AppointmentQuery;
import cn.wolfcode.car.business.service.IAppointmentService;
import cn.wolfcode.car.business.service.IStatementService;
import cn.wolfcode.car.common.base.page.TablePageInfo;
import cn.wolfcode.car.common.exception.BusinessException;
import cn.wolfcode.car.common.util.Convert;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

@Service
@Transactional
public class AppointmentServiceImpl implements IAppointmentService {

    @Autowired
    private AppointmentMapper appointmentMapper;
    @Autowired
    private IStatementService statementService;

    @Override
    public TablePageInfo<Appointment> query(AppointmentQuery qo) {
        PageHelper.startPage(qo.getPageNum(), qo.getPageSize());
        return new TablePageInfo<Appointment>(appointmentMapper.selectForList(qo));
    }


    @Override
    public void save(Appointment appointment) {
        appointment.setCreateTime(new Date());
        appointment.setDeleteFlag(Appointment.DELETEFLAG_NO);
        appointmentMapper.insert(appointment);
    }

    @Override
    public Appointment get(Long id) {
        return appointmentMapper.selectByPrimaryKey(id);
    }


    @Override
    public void update(Appointment appointment) {
        // 只有处于预约中的状态才能进行编辑
        Appointment oldObj = this.get(appointment.getId());
        if (!Appointment.STATUS_APPOINTMENT.equals(oldObj.getStatus())) {
            throw new BusinessException("只有处于预约中的预约单才能进行修改.");
        }
        // 处理特殊字段
        appointmentMapper.updateByPrimaryKey(appointment);
    }

    @Override
    public void deleteBatch(String ids) {
        Long[] dictIds = Convert.toLongArray(ids);
        for (Long dictId : dictIds) {
            // 校验
            Appointment oldObj = this.get(dictId);
            if (!Appointment.STATUS_APPOINTMENT.equals(oldObj.getStatus())) {
                throw new BusinessException("只有处于预约中的预约单才能进行删除.");
            }
            appointmentMapper.deleteflag(dictId,Appointment.DELETEFLAG_YES); // 软删除
            // appointmentMapper.deleteByPrimaryKey(dictId);   //硬删除
        }
    }

    @Override
    public List<Appointment> list() {
        return appointmentMapper.selectAll();
    }

    @Override
    public void cancel(Long id) {
        Appointment appointment = this.get(id);
        if (!appointment.getStatus().equals(Appointment.STATUS_APPOINTMENT)) {
            throw new BusinessException("只有处于预约中才能取消订单");
        }
        appointmentMapper.cancel(id,Appointment.STATUS_CANCEL);
    }

    @Override
    public void arrival(Long id) {
        Appointment appointment = this.get(id);
        if (!appointment.getStatus().equals(Appointment.STATUS_APPOINTMENT)) {
            throw new BusinessException("只有处于预约中才能进行到店");
        }
        appointmentMapper.arrival(id,Appointment.STATUS_ARRIVAL,new Date());
    }

    @Override
    public Statement generateStatement(Long appointmentId) {
        // 获取预约单对象
        Appointment oldObj = this.get(appointmentId);
        // 校验
        if (!(Appointment.STATUS_ARRIVAL.equals(oldObj.getStatus())
                || Appointment.STATUS_SETTLE.equals(oldObj.getStatus())))
        {
            throw new BusinessException("只有处于到店和结算才能进行结算单处理");
        }
        // 根据预约单ID查结算单
        Statement statement = statementService.getByAppointmentId(appointmentId);
        if (statement == null) {
            statement = new Statement();
            statement.setAppointmentId(appointmentId); // 预约单ID
            statement.setCarSeries(oldObj.getCarSeries()); // 汽车分类
            statement.setActualArrivalTime(oldObj.getActualArrivalTime()); // 到店时间
            statement.setCreateTime(new Date()); // 创建时间
            statement.setCustomerName(oldObj.getCustomerName()); // 客户名称
            statement.setCustomerPhone(oldObj.getCustomerPhone()); // 客户电话
            statement.setInfo(oldObj.getInfo()); // 备注
            statement.setServiceType(oldObj.getServiceType()); // 服务类型
            statement.setLicensePlate(oldObj.getLicensePlate()); // 车牌号
            statementService.save(statement);
        }
        return statement;
    }

    @Override
    public void updateStatus(Long appointmentId, Integer status) {
        appointmentMapper.updateStatus(appointmentId,status);
    }
}
